package deposit.main;

import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

public class MonthCapitalizeDeposit extends Deposit {

    public MonthCapitalizeDeposit() {
	super();
    }

    public MonthCapitalizeDeposit(String bank, String depoId, Date startDate, int dayLong, double sum, double interestRate) {
	super(bank, depoId, startDate, dayLong, sum, interestRate);
    }

    public double getInterest() {
	if (interest > ZERO) { 
	    return interest;
	}
	
	int dayN = 0;
	GregorianCalendar calendar = new GregorianCalendar();
	calendar.setTime(startDate);
	int month = calendar.get(Calendar.MONTH);
	int day = calendar.get(Calendar.DAY_OF_MONTH) + 1;
	int daysInDepositYear = getDaysInDepositYear();
	double interest = 0.0;
	double capital = 0.0;
	int periodDay = 0;
	double dayCf = 0.0;
	
	while (true) {
	    month++; // увеличиваем на месяц
	    if (month > 11) {
		month = 0;
	    }

	    calendar.set(Calendar.MONTH, month);    //
	    calendar.set(Calendar.DAY_OF_MONTH, 1); // переходим в первое число месяца
	    
	    calendar.add(Calendar.DATE, -1);        // откидываемся на один день назад
	    periodDay = calendar.get(Calendar.DAY_OF_MONTH) - day + 1;
	    if ((dayN + periodDay) > dayLong) {
		break;
	    }
	    dayCf = periodDay;
	    interest = (sum + capital) * (interestRate / 100.0) * (dayCf / daysInDepositYear);
	    
	    capital += interest;
	    day = 1;
	    dayN += periodDay;
	}
	
	dayCf = dayLong - dayN;
	interest = (sum + capital) * (interestRate / 100.0) * (dayCf / daysInDepositYear);
	capital += interest;
	return capital;
    }
}